﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace EIP.Common.Dapper.Extensions.Common
{
    public class StoredProcedure
    {
        private List<SqlParameter> _SPParams;

        public StoredProcedure(string spName)
        {
            SPName = spName;
        }

        public string SPName { get; set; }

        public List<SqlParameter> SPParams
        {
            get
            {
                if (_SPParams == null)
                {
                    _SPParams = new List<SqlParameter>();
                }
                return _SPParams;
            }
        }

        public int ParamsCount
        {
            get { return SPParams.Count; }
        }

        public object GetParameterValue(string paramName)
        {
            foreach (var p in SPParams)
            {
                if (p.ParameterName == paramName)
                {
                    return p.Value;
                }
            }
            return null;
        }

        public object GetParameterValue(int index)
        {
            if (index < SPParams.Count && index >= 0)
            {
                return SPParams[index].Value;
            }
            return null;
        }

        public void AddParameter(string paramName, object paramValue, DbType dbtype)
        {
            AddParameter(paramName, paramValue, dbtype, ParameterDirection.Input);
        }

        public void AddParameter(string paramName, object paramValue, DbType dbtype, ParameterDirection pDirection)
        {
            var p = new SqlParameter();
            p.DbType = dbtype;
            p.ParameterName = paramName;
            p.Value = paramValue;
            p.Direction = pDirection;
            SPParams.Add(p);
        }
    }
}